/**************************************************************************/
/*                                                                        */
/* This program is free software; you can redistribute it and/or modify   */
/* it under the terms of the GNU General Public License as published by   */
/* the Free Software Foundation; either version 2 of the License, or      */
/* (at your option) any later version.                                    */
/*                                                                        */
/* This program is distributed in the hope that it will be useful,        */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of         */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the           */
/* GNU General Public License for more details.                           */
/*                                                                        */
/* You should have received a copy of the GNU General Public License      */
/* along with this program; if not, write to the                          */
/* Free Software Foundation, Inc.,                                        */
/* 59 Temple Place - Suite 330, Boston, MA  02111-1307  USA               */
/*                                                                        */
/**************************************************************************/
/* Program to test the qgetoptlibrary                                     */
/* ==================================                                     */
/*                                                                        */
/* Command line option that can be used to test the program               */
/*                                                                        */
/* test1 -h                                                               */
/* test1 -v                                    ERROR                      */
/* test1 -v test.log                                                      */
/* test1 -v 1 test.log                                                    */
/* test1 -v 2 test.log                                                    */
/* test1 -v 3 test.log                                                    */
/* test1 -v 7 test.log                                                    */
/* test1 -vb test.bak test.log                                            */
/* test1 -v 7 -b test.bak test.log                                        */
/* test1 -dvb test.bak test.log                                           */
/* test1 -dv 7 -b test.bak test.log                                       */
/* test1 --echo                                ERROR                      */
/* test1 --echo "Hello World"                                             */
/*                                                                        */
/**************************************************************************/
#include "optManager.h"
#include <QtCore>

void setOptions(QString ProgName);
int verboseLevel=0;
bool display = false;
QFile * file = 0;

void print(QString data)
{
   QTextStream console(stdout);
   console << data << "\n";
   if ((verboseLevel & 4) && file != 0)
   {
      QTextStream out (file);
      out << data << "\n";
   }
}

void verbose(const OptSwitch * opt)
{
   QStringList arg = opt->argList();
   int level;
   bool ok = true;
   if (arg.isEmpty())
      level = 1;
   else
      level = arg[0].toInt(&ok,10);
   if (!ok)
      level = 1;
   verboseLevel = level;
}

void echo(const OptSwitch * opt)
{
   QStringList args = opt->argList();
   QString TEXT;
   if (args.isEmpty())
      TEXT = "No details provided -- should never get here.";
   else
      TEXT = args[0];
   print(TEXT);
}

void printLogFile(const OptSwitch *)
{
   display = true;
}

void backup(const OptSwitch * opt)
{
   OptManager * om = OptManager::getOptMgr();
   QString oldFileName = om->getAppArg(0);
   if (!QFile::exists(oldFileName))
   {
      print ("\"" + oldFileName + "\" doesn't exist, nothing to back up.\n");
      return;
   }
   QStringList args = opt->argList();
   QString fileName;
   if (args.isEmpty())
      print ("FAILED the name of the archive was not provided -- should never get here.\n");
   else
   {
      QString newFile = QFileInfo(args[0]).absoluteFilePath();

      QFile::remove(newFile);
      if (QFile::rename(oldFileName, newFile))
         print ("Made a backup of: " + oldFileName + " at " + newFile + "\n");
      else
         print ("FAILED to make a backup of " + oldFileName + "\n");
   }
}

int main(int argc, char *argv[])
{
   setOptions(argv[0]);
   OptManager * om = OptManager::getOptMgr();
   om->parse(argc, argv);

   if (verboseLevel & 4)
   {
      file = new QFile(om->getAppArg(0));
      if (display)
      {
         if (!file->open(QIODevice::ReadWrite))
            print(QObject::tr("Failed to open file for logging."));
         QTextStream console(stdout);
         console << file->readAll();
      }
      else
      {
         if (!file->open(QIODevice::WriteOnly))
            print(QObject::tr("Failed to open file for logging."));
      }
   }
   
   if (verboseLevel & 1)
      print(om->appArgDump());
   if (verboseLevel & 2)
      print(om->switchDump());
   
   if (file != 0)
      file->close();
   om->close();
   return 0;
}

void setOptions(QString ProgName)
{
   QString detail;
   OptManager * om = OptManager::getOptMgr(ProgName);
   om->addAppArgs("FILE");
   detail = QObject::tr("This program is a test program for the \"qgetopt\" library. It will accept various command line options and requires the name of a file to be passed in.");
   om->addDescription(detail);
   detail = QObject::tr("This is the level of detail that will be written, by default to the console but it can also be written to the file. 0 no detail. 1 the program argument passed in. 2 the options and their value. Add 4 to any of the previous levels and the details will also be written to the file.");
   om->addSwitch("verbose",verbose, 'v', "[LEVEL]")->addDescription(detail);
   detail = QObject::tr("This will echo TEXT to the console.");
   om->addSwitch("echo",echo, ' ', "TEXT",0,immediateStop)->addDescription(detail);
   detail = QObject::tr("This will display the details of the log file before any changes to it.");
   om->addSwitch("detail",printLogFile, 'd')->addDescription(detail);
   detail = QObject::tr("This will backupthe log file before any changes are made to it.");
   om->addSwitch("back-up",backup, 'b', "FILE")->addDescription(detail);
}
